package com.lfnyxy.service.imp;

import com.lfnyxy.common.FileUploadTool;
import com.lfnyxy.common.cnst.AppString;
import com.lfnyxy.dao.ConfigurationDao;
import com.lfnyxy.dao.FileDao;
import com.lfnyxy.entity.ConfigurationEntity;
import com.lfnyxy.entity.FileEntity;
import com.lfnyxy.service.FileUploadService;
import com.lfnyxy.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.Enumeration;
import com.oreilly.servlet.MultipartRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description:
 */
@Service
public class FileUploadServiceImp implements FileUploadService {

    @Autowired
    private ConfigurationDao configurationDao;

    @Autowired
    private FileDao fileDao;

    public FileEntity fileUpload(MultipartFile file) {

        //获得上传文件名
        String fileName=file.getOriginalFilename();

        //获取文件扩展名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));

        //根据扩展名获取文件保存路径
        String path = null;

        if (suffixName.equals(".jpg")||suffixName.equals(".png")) {
            path = configurationDao.QueryItemByKey(AppString.IMAGE_FILE).getValue();
        } else if(suffixName.equals(".xls")||suffixName.equals(".xlsx")) {
            path = configurationDao.QueryItemByKey(AppString.DOC_FILE).getValue();
        }

        String  saveDiskName = FileUploadTool.FileUpload(file,suffixName,path);

        FileEntity fileEntity = new FileEntity();
        fileEntity.setName(fileName);
        fileEntity.setRealName(saveDiskName);
        fileEntity.setPath(path);
        fileEntity.setExtend(suffixName);

        fileDao.CreateItem(fileEntity);

        return fileEntity;
    }

}
